<?php

 include 'header.php';
    
//echo "hello world";
$debug=0;
$fad=$_POST['fad'];
$lad=$_POST['lad'];
$gen=$_POST['gen'];

$label=$_POST['Labels'];
echo "label is $label<br>";
$radio_sort=$_POST['radio2'];
echo "radio is $radio_sort";

echo "gen is $gen";

$arrgen=explode(',', $gen);
$arrfad=explode(',',$fad);
$arrlad=explode(',',$lad);
if($debug){
echo "fad is $fad<br><br>";
echo "lad is $lad<br><br>";
}

if($label =="Labels"){
	for($i=0;$i< sizeof($arrfad)-1;$i++){
		$scatterp[$i]=$arrlad[$i]+($arrfad[$i]-$arrlad[$i])/2;
		echo "scatterp is $scatterp[$i]<br>";

	}
}

if($radio_sort=="FAD"){
	if($label =="Labels"){
		array_multisort($arrfad, $arrlad, $arrgen,$scatterp);
	}
	else{
		array_multisort($arrfad, $arrlad, $arrgen);
	}
}
if($radio_sort=="LAD"){
	if($label =="Labels"){
		array_multisort($arrlad, $arrfad, $arrgen,$scatterp);
	}
	else{
		array_multisort($arrlad, $arrfad, $arrgen);
	}
}
if($debug){
	for($i=0;$i<sizeof($arrfad);$i++){
		echo "fad is $arrfad[$i] and lad is $arrlad[$i] and gen is $arrgen[$i]<br>";
	}
}

//$File = "../../../admin1/list/graph/rangegraph.php";

$File = "../../../graph/rangegraph.php";
$Handle = fopen($File, 'w');

$Data = "<?php
    include (\"./src/jpgraph.php\");
    include (\"./src/jpgraph_error.php\");
    include (\"./src/jpgraph_scatter.php\");";
    fwrite($Handle, $Data);
	if($label =="Labels"){
		$Data="\$scattery=array";
		fwrite($Handle, $Data);
		$scatterbar="($scatterp[0]";
		for($i=1;$i<sizeof($scatterp);$i++){
			$scatterbar=$scatterbar.",".$scatterp[$i];
		}
		$scatterbar=$scatterbar.")";
		$len=sizeof($scatterp);
		$Data="$scatterbar;
		
		for(\$i=0; \$i <$len ; ++\$i ) {
	    \$targ1[\$i] = \"http://www.yahoo.com\"; 	    
	    \$alts1[\$i] = \"val=\$i\";
	    
		}
		
		
		
		";
		fwrite($Handle, $Data);
	}
    

    $Data ="\$datay=array";
    fwrite($Handle, $Data);
    if($radio_sort == "FAD" || $radio_sort == "LAD"){
		$barval="($arrfad[1],$arrlad[1]";
		for($tmp=2; $tmp<sizeof($arrfad); $tmp++){    
		$barval =$barval.",".$arrfad[$tmp].",".$arrlad[$tmp];
		}
    }
    else{
		$barval="($arrfad[0],$arrlad[0]";
		for($tmp=1; $tmp<sizeof($arrfad)-1; $tmp++){    
		$barval =$barval.",".$arrfad[$tmp].",".$arrlad[$tmp];
		}
    
    
    }
    $barval=$barval.")";
    if($debug) {echo "barvalue is".$barval;}
    $Data="$barval;

    
    \$graph = new Graph(600,400,\"auto\");    
	\$graph->SetScale(\"textlin\");

	\$graph->img->SetMargin(40,30,20,80);
	\$graph->SetShadow();";
	fwrite($Handle,$Data);
	if($label=="Labels"){
		$Data="
		\$sp1 = new ScatterPlot(\$scattery,\$datax);
		\$sp1->SetCSIMTargets(\$targ1,\$alts1);

		\$graph->Add(\$sp1);";
		fwrite($Handle,$Data);
	}

	$Data="
	\$errplot=new ErrorPlot(\$datay);
	\$errplot->SetColor(\"red\");
	\$errplot->SetWeight(2);
	\$errplot->SetCenter();

	
	\$graph->Add(\$errplot);

	\$graph->title->Set(\"Stratigraphic Range\");
	\$graph->xaxis->title->Set(\"X-title\");
	\$graph->yaxis->title->Set(\"Y-title\");

	\$graph->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	\$graph->xaxis->Hide();
	\$datax = \$gDateLocale->GetShortMonth();
	\$graph->xaxis->SetTickLabels(\$datax);";
	fwrite($Handle,$Data);
	
	if($label=="Labels"){
	$Data="
	\$graph->StrokeCSIM('auto');
	?>";
	fwrite($Handle, $Data);
	
	}
	else{
	$Data="
	\$graph->Stroke();
    
    ?>";  
    
    
    fwrite($Handle, $Data);
    }
    
   fclose($Handle);
    //$myFile = "../../../admin1/list/graph/jpgraph_cache/rangegraph.png";
    
    $myFile = "../../../graph/jpgraph_cache/rangegraph.png";
  unlink($myFile);
  
  
  //echo "<img src=\"http://localhost/test/admin1/list/graph/rangegraph.php\">";
  
   echo "<img src=\"http://www.fossilplot.org/test/graph/rangegraph.php\">";
  
  include 'footer.php';
  
  

?>